package com.freshcity.page.talk
{
	import com.freshcity.component.container.Component;
	import com.freshcity.core.OpCodes;
	import com.freshcity.guanzhu.GuanZhuWriteWin;
	import com.freshcity.manager.LayerManager;
	import com.freshcity.utils.StringUtil;
	
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	public class GuanZhuWrite extends Component
	{
		public var talk:Talk;
		private var back:GuanZhuWriteWin;
		
		public function GuanZhuWrite(parent:DisplayObjectContainer=null, xpos:Number=0, ypos:Number=0)
		{
			this._width=240;
			this._height=100;
			super(parent, xpos, ypos);
			back.closeBtn.addEventListener(MouseEvent.CLICK,hide);
			back.submitBtn.addEventListener(MouseEvent.CLICK,onSubmit);
		}
		
		protected function onSubmit(event:MouseEvent):void
		{
			var c:String = StringUtil.trim(back.words.text);
			if(c.length>0){
				talk.main.commu.send(OpCodes.GUANZHU_TA,{nikeName:c,toUser:talk.toUser.id},onCallBack);
			}else{
				talk.main.msg.showRollTipsMsg("填写内容为空");
			}
		}
		
		private function onCallBack(data:*):void
		{
			if(data is String){
				talk.main.msg.showRollTipsMsg(data);
			}else{
				talk.main.msg.showRollTipsMsg("操作成功");
				talk.main.friendPanel.addFriend(data);
				talk.win.addTaCheckBox.visible = false;
				talk.win.giftBtn.visible = true;
				hide();
			}
		}
		
		override public function destroy():void
		{
			back.closeBtn.removeEventListener(MouseEvent.CLICK,hide);
			back.submitBtn.removeEventListener(MouseEvent.CLICK,onSubmit);
			super.destroy();
		}
		
		override protected function addChildren():void
		{
			super.addChildren();
			back = new GuanZhuWriteWin();
			addChild(back);
		}
		
		override public function show(event:Event=null):void
		{
			if(!this.parent){
				LayerManager.popupLayer.addChild(this);
				this.x = (main.stage.stageWidth - width)/2;
				this.y = (main.stage.stageHeight - height)/2;
			}
			super.show(event);
		}
		
	}
}